import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {postRequest} from "@/utils/api";
import {putRequest} from "@/utils/api";
import {getRequest} from "@/utils/api";
import {deleteRequest} from "@/utils/api";
import { initMenu } from './utils/menus';
import 'font-awesome/css/font-awesome.css';
import { downloadRequest } from "@/utils/download";
Vue.config.productionTip = false
Vue.use(ElementUI, {size : 'small'});
// 插件形式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;
// to 是你要即将进入的路由
// form 从当前导航要离开的路由
// next 进行跳转
router.beforeEach((to, from,next) => {
  //判断用户有没有登录 / 初始化菜单
  if (window.sessionStorage.getItem("tokenStr")) {
    initMenu(router, store);
    // 如果用户不存在
    if (!window.sessionStorage.getItem('user')) {
       // 判断用户信息是否存在
       getRequest('/admin/info').then(resp => {
        if (resp) {
           // 存入用户信息
          window.sessionStorage.setItem('user', JSON.stringify(resp))
          store.commit("INIT_ADMIN",resp);
          next();
          // next();
        }
      })
    }
    next();
  } else {
    // 如果他是要去登录页就去登录页
    if (to.path == '/') {
      next();
    } else {
      // 如果一上来是别的路径那我们先把它拦到登录页，等登录成功后在跳到它写的路径
      next('/?redirect=' + to.path);
    }
  }
  // console.log(to)
  // console.log(from)
  // next();
})

new Vue({
  router,
  store,
  render: h => h(App)
  // 手动挂载
}).$mount('#app')
